<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>socket服务</title>
    </title>
</head>

<body>
    <button id="connect">连接服务器</button>
    <button id="send" disabled="true">发送数据</button>
    <p>服务器发送的数据如下</p>
    </p>
    <span id="content"></span>

</body>
<script>
    var connect = document.querySelector("#connect");
    var send = document.querySelector("#send")
    var content = document.getElementById("content")
    var ws = null;
    connect.onclick = () => {
        ws = new WebSocket("ws://localhost:9999")
        ws.onopen = () => {
            console.log("连接成功")
            send.disabled = false;
        }
        ws.onmessage = msg => {
            const reader = new FileReader()
            console.log("收到服务器 的信息")
            console.log(msg)
            let result =null;
            if(msg.data instanceof Blob){
                reader.readAsText(msg.data,'UTF-8');
                reader.onload=e=>{
                    result = JSON.parse(reader.result);
                    console.log(result)
                    content.innerHTML = result
                }
            }else{
                content.innerHTML = msg.data
            }
            
        }
        ws.onclose = msg => {
            console.log("服务器关闭")
            send.disabled = true
        }


    }

    send.onclick = function () {
        ws.send(JSON.stringify({
            "action": "a",
            "socketType": "fullScreen",
            "chartName": "trend",
            "value": "true",
        }))
    }
</script>

</html>